package com.lanou.dllo.shise.com.lanou.dllo.fragment;


import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.dllo.shise.R;

import com.lanou.dllo.shise.com.lanou.dllo.adapter.Carefully_chooseAdapter;
import com.lanou.dllo.shise.com.lanou.dllo.adapter.Close_attentionListAdapter;
import com.lanou.dllo.shise.com.lanou.dllo.adapter.SameCityListAdapter;
import com.lanou.dllo.shise.com.lanou.dllo.entity.ChooseBean;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class Carefully_choseFragment extends Fragment {
    private RequestQueue queue;
    private RecyclerView fragmentCarefullychooseView;
    private Carefully_chooseAdapter adapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_carefully_choose, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        queue = Volley.newRequestQueue(getActivity());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),2);
        fragmentCarefullychooseView = (RecyclerView) getActivity().findViewById(R.id.fragmentCarefullychooseView);
        fragmentCarefullychooseView.setLayoutManager(gridLayoutManager);
        adapter = new Carefully_chooseAdapter(getActivity());
        fragmentCarefullychooseView.setAdapter(adapter);

    }

    private void initData() {
        String path = "http://api.wecook.cn/v3/recipe/search?timestamp=1445684029068&sign=f091d3642dc87cf634dea149308c8ffb&uid=502685&keywords=%E5%BF%AB%E6%89%8B%E8%8F%9C&wid=f06f82c3d0f511b0d7c1bd250e194106&page=2&batch=20&net=wifi&access=259eea423dee18c7b865b0777cd657cc";
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ChooseBean chooseBean = gson.fromJson(response, ChooseBean.class);
                if (chooseBean != null) {
                    List<ChooseBean.ResultEntity.ListEntity> li = chooseBean.getResult().getList();
                    if (li != null && li.size() > 0) {
                        adapter.addData(li);

                        }
                    }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }

            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        queue.add(request);
    }

}
